package org.hxzon.rpggame.test;

import io.netty.buffer.ByteBuf;

import java.nio.charset.Charset;

public class Util {

    public static final String encoding = "UTF-8";
    public static final Charset charset = Charset.forName(encoding);
    public static final int serverPort = 8888;
    public static final int adminServerPort = 9999;
    public static final int MaxFrameLength = 10240;//10k
    public static final int LengthFieldLength = 4;

    public static String readString(ByteBuf byteBuf) {
        try {
            short len = byteBuf.readShort();
            byte[] strBytes = new byte[len];
            byteBuf.readBytes(strBytes);
            return new String(strBytes, encoding);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void writeString(ByteBuf byteBuf, String str) {
        try {
            byte[] strBytes = str.getBytes(encoding);
            byteBuf.writeShort(strBytes.length);
            byteBuf.writeBytes(strBytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static int readCmd(ByteBuf byteBuf) {
        return byteBuf.readInt();
    }

    public static void writeCmd(ByteBuf byteBuf, int cmd) {
        byteBuf.writeInt(cmd);
    }

    public static int readLength(ByteBuf byteBuf) {
        return byteBuf.readInt();
    }
}
